package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.vo.AddressVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private TbAddressMapper tbAddressMapper;
    @Autowired
    private TbProvincesMapper tbProvincesMapper;
    @Autowired
    private TbCitiesMapper tbCitiesMapper;
    @Autowired
    private TbAreasMapper tbAreasMapper;

    @Override
    public List<AddressVo> getAddressByUserId(String username) {
        return tbAddressMapper.getAddressByUserId(username);
    }

    @Override
    public List<TbProvinces> listProvinces() {
        return tbProvincesMapper.selectByExample(null);
    }

    @Override
    public List<TbCities> listCities(String provinceid) {
        TbCitiesExample example = new TbCitiesExample();
        TbCitiesExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceidEqualTo(provinceid);
        return tbCitiesMapper.selectByExample(example);
    }

    @Override
    public List<TbAreas> listArea(String cityid) {
        TbAreasExample example = new TbAreasExample();
        TbAreasExample.Criteria criteria = example.createCriteria();
        criteria.andCityidEqualTo(cityid);
        return tbAreasMapper.selectByExample(example);
    }

    @Override
    public void addAddress(TbAddress tbAddress) {
        tbAddressMapper.insertSelective(tbAddress);
    }

    @Override
    public void updateDef(Long id, String userId) {
        tbAddressMapper.updateNoDefById(id,userId);
        tbAddressMapper.updateDefById(id,userId);
    }

    @Override
    public TbAddress selectById(Long id) {
        return tbAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateAddress(TbAddress tbAddress) {
        tbAddressMapper.updateByPrimaryKey(tbAddress);
    }

    @Override
    public void delAddress(Long id) {
        tbAddressMapper.deleteByPrimaryKey(id);
    }
}
